<?php

namespace app\api\model;

use app\common\exception\BaseException;
use app\common\model\Leader as LeaderModel;

/**
 * 领导模型
 * Class Leader
 * @package app\api\model
 */
class Leader extends LeaderModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'is_delete',
        'wxapp_id'
    ];

    /**
     * 领导详情：HTML实体转换回普通字符
     * @param $value
     * @return string
     */
    public function getContentAttr($value)
    {
        return htmlspecialchars_decode($value);
    }

    public function getViewTimeAttr($value, $data)
    {
        return date('Y-m-d', $data['create_time']);
    }

    /**
     * 领导详情
     * @param $Leader_id
     * @return LeaderModel|null
     * @throws BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public static function detail($Leader_id)
    {
        if (!$model = parent::detail($Leader_id)) {
            throw new BaseException(['msg' => '领导不存在']);
        }
        return $model;
    }

    /**
     * 获取领导列表
     * @param int $limit
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($searchInput = "", $category_id = 0, $limit = 1000,$shanghui_id=0)
    {
        $searchInput != "" && $this->where('name', 'like', '%' . $searchInput . '%');
        $category_id > 0 && $this->where('category_id', '=', $category_id);
        return $this->field(['content'], true)
            ->with(['logo',"category"])
            ->where('is_delete', '=', 0)
            ->where('shanghui_id', '=', $shanghui_id)
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($limit, false, ['query' => \request()->request()]);
    }

}